package com.example.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/response")
@Controller

public class ResponseController {

    /**
     * MVC  原始模型（含有视图）： 只有 Controller
     * @return
     */

    @RequestMapping("/demo1")
    public  String demo1() {
        return "/hello.html";
    }

    /**
     * @ResponseBody + @Controller（有视图） = @RestController(无视图)
     * @ResponseBody 含有的类注释和方法注释， 前者作用域更大， 后者更小。
     * @return
     */

    @ResponseBody
    @RequestMapping("/demo2")
    public  String demo2() {
        return "/hello.html";
    }



//    返回一个html
    @RequestMapping("/returnHtml")
    @ResponseBody
    public  String returnHtml() {
//        如果是视图传输也是404
        return "<h1>返回一个HTML的一级标题</h1>";
    }


//    设置状态码

    /**
     * 设置状态码， 利用响应 HttpServletResponse 来设置
     * @param response
     * @return
     */
    @RequestMapping("/setStatus")
    @ResponseBody
    public  Student setStatsus(HttpServletResponse response) {
        Student student = new Student();
        student.setName("dalao");
        student.setAge(18);
        student.setGender("nv");

        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

        return student;
    }


    /**
     * 设置报文格式
     */
    @RequestMapping(value = "/setContentType",produces =  "application/json")
    @ResponseBody
    public String SetHeader() {
        return "{\"success\":true}";
    }


    /**
     * 设置报头的键值对
     */
    @RequestMapping("/setHeader")
    @ResponseBody
    public  String SetHeader2(HttpServletResponse response) {
            response.setHeader("My-Key", "My-Value");
            return  "键值对设置完成!";
    }

}

